<!--
 * @Description:
 * @Author: Morgan
 * @Date: 2021-06-24 17:07:01
 * @LastEditors: Morgan
 * @LastEditTime: 2021-06-29 19:29:09
-->
<template>
  <!-- 发布资讯页面 -->
  <div>
    <!-- {{ form }} -->
    <el-form ref="ruleForm" :model="form" label-width="60px" :rules="rules">
      <el-row>
        <el-col :span="16">
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" />
          </el-form-item>
          <el-form-item label="分类" prop="articleCategoryId">
            <el-select v-model="form.articleCategoryId">
              <el-option v-for="item in articleCategory" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="封皮" prop="photoId">
            <el-upload
              class="avatar-uploader"
              :action="uploadFileURL"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" />
              <div slot="tip" class="el-upload__tip">文件大小不允许超过3M</div>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="正文" prop="content">
        <tinymce v-model="form.content" :height="240" />
      </el-form-item>

      <el-form-item>
        <div style="text-align:right">
          <el-button type="text" @click="back">返回</el-button>
          <el-button type="primary" @click="toPublish">发布</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import { findAll } from '@/api/infoCategory'
import { showFileURL, uploadFileURL } from '@/utils/config'
import Tinymce from '@/components/Tinymce'
import { saveOrUpdate } from '@/api/article'

export default {
  components: { Tinymce },

  data() {
    return {
      showFileURL,
      uploadFileURL,
      imageUrl: '',
      form: {},
      rules: {
        articleCategoryId: [{ required: true, message: '请选择栏目', trigger: 'change' }],
        title: [{ required: true, message: '请输入文章标题', trigger: 'change' }],
        content: [{ required: true, message: '请输入文章内容', trigger: 'change' }]
      },
      articleCategory: []
    }
  },
  computed: {},
  created() {
    this.form = this.$route.query
    this.queryCategoryData()
  },
  mounted() {},
  methods: {
    // 查询分类信息
    async queryCategoryData() {
      const res = await findAll()
      this.articleCategory = res.data
    },
    // 图片上传成功的回调函数
    handleAvatarSuccess(res, file) {
      // console.log(res,"==图片上传成功的res");
      this.imageUrl = URL.createObjectURL(file.raw)
      // console.log(this.imageUrl);
      this.form.photo = this.showFileURL + res.data.id
    },
    // 图片上传之前的回调函数
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    back() {
      this.$router.go(-1)
    },
    toPublish() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          const res = await saveOrUpdate(this.form)
          // 提示用户保存成功，刷新表格数据，关闭模态框
          this.$notify.success({
            title: '成功',
            message: res.success
          })
          this.back()
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style scoped>
</style>
